package com.cskaoyan.weixin.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User1;
import com.cskaoyan.weixin.bean.address.Address;
import com.cskaoyan.weixin.bean.address.AddressDeleteVO;
import com.cskaoyan.weixin.bean.address.AddressDetailVO;
import com.cskaoyan.weixin.bean.address.AddressVO;
import com.cskaoyan.weixin.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/wx/address/")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("/list")
    public BaseRespVo list() {
        Subject subject = SecurityUtils.getSubject();
        User1 user1 = (User1) subject.getPrincipal();
        Integer id = user1.getId();
        List<AddressVO> addressVOList = wxAddressService.list(id);
        return BaseRespVo.ok(addressVOList);
    }

    @RequestMapping("/detail")
    public BaseRespVo detail(Integer id) {
        AddressDetailVO addressDetailVO = wxAddressService.detail(id);
        return BaseRespVo.ok(addressDetailVO);
    }

    @RequestMapping("/save")
    public BaseRespVo save(@RequestBody Address address){
        int id = wxAddressService.save(address);
        return BaseRespVo.ok(id);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody  AddressDeleteVO addressDeleteVO) {
        wxAddressService.delete(addressDeleteVO.getId());
        return BaseRespVo.ok();
    }
}
